/*
 * @Author: cl
 * @Description:
 * @Date: 2023-04-11 15:26:25
 * @LastEditTime: 2023-04-11 17:02:19
 * @FilePath: \jsnode\nestjs\naota\src\tiger\tiger.pipe.ts
 * @LastEditors: cl
 */
import { ArgumentMetadata, Injectable, PipeTransform, HttpException, HttpStatus } from '@nestjs/common'
import { plainToInstance } from 'class-transformer'
import { validate } from 'class-validator'

@Injectable()
export class TigerPipe implements PipeTransform {
  async transform(value: any, metadata: ArgumentMetadata) {
    const dto = plainToInstance(metadata.metatype, value)
    const errmsg = await validate(dto)
    console.log(errmsg)
    if (errmsg.length) {
      throw new HttpException(errmsg, HttpStatus.BAD_REQUEST)
    }

    return value
  }
}
